﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Data;
using System.Windows.Shapes;
using System.Threading.Tasks;

namespace PracaInz_v0
{
    class ExpandTree
    {
        public Button exp = new Button();           // guzik
        Button expBack = new Button();              // guzik powrotny
        DataRow StartPoint;                         // dane skad zaczac nowe drzewo
        DataRow BackPoint;
        int Levels = 0;

        // trzeba policzyc ile bedzie poziomow od dr do statniego z jego potomkow 
        public static System.Windows.Thickness thc = new System.Windows.Thickness(-0.5, -5.5, -6, -3);
        public static System.Windows.Thickness thcBack = new System.Windows.Thickness(1.5, -15.5, -6, -3);

        DrawTree _DT;

        public ExpandTree(double top, double left, DataRow row, DrawTree DT)
        {
            this._DT = DT;
            this.StartPoint = row;
            this.exp.Content = "+";
            this.exp.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Left;
            this.exp.FontSize = 10;
            this.exp.Foreground = System.Windows.Media.Brushes.CornflowerBlue;
            this.exp.Width = 10;
            this.exp.Height = 10;
            this.exp.Padding = thc;
            Canvas.SetTop(this.exp, top + (this.exp.Width / 2));
            Canvas.SetLeft(this.exp, left - (this.exp.Width / 2));

            this.countLevels(this.StartPoint);

            this.Levels = this.Levels - Convert.ToInt32(this.StartPoint[1]) + 1;
            if (this.Levels > this._DT._TVS.TreeViewLevel)
                this.Levels = this._DT._TVS.TreeViewLevel;

            this.exp.Click += new System.Windows.RoutedEventHandler(this.exp_Click);
        }

        void countLevels(DataRow dr)
        {
            if (this.Levels < Convert.ToInt32(dr[1]))
                this.Levels = Convert.ToInt32(dr[1]);
            if (Convert.ToInt32(dr[4]) != 0)
            {
                this.countLevels(this._DT.TreeData.Rows[Convert.ToInt32(dr[4]) - 1]);
            }

            if (Convert.ToInt32(dr[5]) != 0)
            {
                this.countLevels(this._DT.TreeData.Rows[Convert.ToInt32(dr[5]) - 1]);
            }
        }

        void back()
        {
            int l = Convert.ToInt32(this.StartPoint[1]) - this._DT._TVS.TreeViewLevel;
            if (l <= 0)
            {
                this.BackPoint = this._DT.TreeData.Rows[0];
            }
            else
            {
                l = Convert.ToInt32(this.StartPoint[1]) - l - 1;
                for (int i = l; i > 0; i--)
                {
                    int p = Convert.ToInt32(this.BackPoint[3]) - 1;
                    if (p < 0)
                    {
                        break;
                    }
                    this.BackPoint = this._DT.TreeData.Rows[p];
                }
            }
        }

        void exp_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            this.BackPoint = this.StartPoint;
            this._DT._TVS.SliderValue = 1;
            this._DT.TreeCanvas.Children.Clear();

            countLevels(this.StartPoint);

            this.Levels = this.Levels - Convert.ToInt32(this.StartPoint[1]) + 1;
            if (this.Levels > this._DT._TVS.TreeViewLevel)
                this.Levels = this._DT._TVS.TreeViewLevel;

            this._DT.TreeCanvas.Height = this._DT.pdistH * this.Levels;
            this._DT.TreeCanvas.Width = this._DT.TreeCanvas.Height * 1.2;

            newCan();

            this._DT.first = true;
            this._DT.DrawNodes(this._DT.TreeCanvas.Width, this.StartPoint, (this._DT.TreeCanvas.Width / 2), 1);
        }

        void expBack_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            this._DT._TVS.SliderValue = 1;
            this._DT.TreeCanvas.Children.Clear();
            this._DT.TreeCanvas.Height = this._DT.pdistH * this._DT._TVS.TreeViewLevel;
            this._DT.TreeCanvas.Width = this._DT.TreeCanvas.Height * 1.2;

            back();

            if (Convert.ToInt32(this.BackPoint[0]) != 1)
                newCan();

            this._DT.first = true;
            this._DT.DrawNodes(this._DT.TreeCanvas.Width, this.BackPoint, (this._DT.TreeCanvas.Width / 2), 1);
        }

        void newCan()
        {
            Line line = new Line();
            line.Stroke = System.Windows.Media.Brushes.YellowGreen;
            line.X1 = _DT.TreeCanvas.Width / 2;
            line.X2 = _DT.TreeCanvas.Width / 2;
            line.Y1 = 0;
            line.Y2 = _DT.pdistH / 2;
            line.StrokeThickness = 1;
            Canvas.SetZIndex(line, -10);
            this._DT.TreeCanvas.Children.Add(line);

            this.expBack.Content = "_";
            this.expBack.HorizontalContentAlignment = System.Windows.HorizontalAlignment.Left;
            this.expBack.FontSize = 12;
            this.expBack.Foreground = System.Windows.Media.Brushes.CornflowerBlue;
            this.expBack.Width = 12;
            this.expBack.Height = 12;
            this.expBack.Padding = thcBack;
            Canvas.SetTop(expBack, (this._DT.pdistH / 4) - (this.expBack.Height / 2));
            Canvas.SetLeft(this.expBack, (this._DT.TreeCanvas.Width / 2) - (this.expBack.Width / 2));
            this._DT.TreeCanvas.Children.Add(this.expBack);

            this.expBack.Click += new System.Windows.RoutedEventHandler(this.expBack_Click);

        }
    }
}
